package com.hjm.ui.home.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hjm.ui.R;
import com.hjm.ui.home.entity.Goods;

import java.util.List;

public class HomeGoodsItemAdapter extends RecyclerView.Adapter<HomeGoodsItemAdapter.ViewHolder> {
    private List<? extends Goods> goodsList;
    private Context context;
    private Listener listener;

    public <T extends Goods> HomeGoodsItemAdapter(List<T> goodsList, Listener listener,Context context) {
        this.goodsList = goodsList;
        this.context = context;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_goods, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Goods goods = goodsList.get(position);
        holder.image.setImageDrawable(goods.getFrontImg());
//        holder.caption.setText(goods.getCaption());
//        holder.caption.setVisibility(View.GONE);
        holder.name.setText(goods.getName());
        holder.price.setText(goods.getPrice());
        holder.parent.setOnClickListener((v)-> {
            if (listener != null)
                listener.onClick(goods);
        });
    }

    @Override
    public int getItemCount() {
        return goodsList.size();
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView caption;
        TextView name;
        TextView price;
        ViewGroup parent;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
//            caption = itemView.findViewById(R.id.caption);
            name = itemView.findViewById(R.id.name);
            price = itemView.findViewById(R.id.price);

            parent = itemView.findViewById(R.id.item_home_goods);
        }
    }

    public interface Listener{
        void onClick(Goods goods);
    }
}
